<template>
  <div class="post">
    <div class="container">
      <div class="search-card">
        <el-button
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:post:add']"
          >{{ $t("system.Add") }}</el-button
        >
      </div>

      <el-table
        v-loading="loading"
        :data="postList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          :label="$t('system.PostName')"
          align="center"
          prop="postName"
          min-width="150"
        />
        <el-table-column
          :label="$t('system.PostSort')"
          align="center"
          prop="postSort"
          min-width="150"
        />
        <el-table-column
          :label="$t('system.Status')"
          align="center"
          prop="status"
          min-width="150"
        >
          <template #default="{ row }">
            <span class="tag" v-if="row.status === '1'">{{
              $t("system.Stop")
            }}</span>
            <span class="tag success" v-if="row.status === '0'">{{
              $t("system.Normal")
            }}</span>
          </template>
        </el-table-column>
        <el-table-column
          :label="$t('system.CreateTime')"
          align="center"
          prop="createTime"
          min-width="150"
        >
          <template #default="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column
          :label="$t('system.Operate')"
          min-width="180"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template #default="{ row }">
            <el-button
              link
              type="primary"
              @click="handleUpdate(row)"
              v-hasPermi="['system:post:edit']"
              >{{ $t("common.edit") }}</el-button
            >
            <el-button
              link
              type="danger"
              @click="deletePrompt(row.postId, row.postName)"
              v-hasPermi="['system:post:remove']"
              >{{ $t("common.delete") }}</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
        class="pagination"
      />

      <!-- 添加或修改职位对话框 -->
      <el-dialog
        :title="title"
        v-model="open"
        width="500px"
        append-to-body
        class="add-dialog"
      >
        <el-form ref="postRef" :model="form" :rules="rules" label-width="100px">
          <el-form-item :label="$t('system.PostName')" prop="postName">
            <el-input v-model="form.postName" placeholder="请输入职位名称" />
          </el-form-item>

          <el-form-item :label="$t('system.PostSort')" prop="postSort">
            <el-input-number
              v-model="form.postSort"
              controls-position="right"
              :min="0"
              :max="999"
            />
          </el-form-item>
          <el-form-item :label="$t('system.Status')" prop="status">
            <el-select v-model="form.status" placeholder="请选择职位状态">
              <el-option
                v-for="dict in sys_normal_disable"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('system.Remark')" prop="remark">
            <el-input
              v-model="form.remark"
              type="textarea"
              placeholder="请输入内容"
            />
          </el-form-item>
          <div class="dialogFoot">
            <el-button class="btn delete" @click="cancel">{{
              $t("common.cancel")
            }}</el-button>
            <el-button class="btn search" @click="submitForm">{{
              $t("common.confirm")
            }}</el-button>
          </div>
        </el-form>
      </el-dialog>
    </div>

    <!-- 删除弹出层 -->
    <el-dialog v-model="deleteVisible" class="no-header-dialog">
      <img src="@/assets/images/warn.png" alt="" />
      <div class="dec">
        删除后不可恢复，确定删除<span class="deletecolor">{{ deleteName }}</span
        >吗？
      </div>
      <div class="chooise">
        <div class="btn delete" @click="closeDelete">取消</div>
        <div class="btn success" @click="deleteApply">确定</div>
      </div>
    </el-dialog>
  </div>
</template>

<script setup name="Post">
import {
  listPost,
  addPost,
  delPost,
  getPost,
  updatePost,
} from "@/api/system/post";
import { reactive } from "vue";
import { ElMessage } from "element-plus";
import { getCurrentInstance } from "vue";

const { $t } = getCurrentInstance().proxy;
const { proxy } = getCurrentInstance();
const { sys_normal_disable } = proxy.useDict("sys_normal_disable");

const postList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    postName: undefined,
    status: undefined,
  },
  rules: {
    postName: [
      { required: true, message: "职位名称不能为空", trigger: "blur" },
    ],
    postSort: [
      { required: true, message: "职位顺序不能为空", trigger: "blur" },
    ],
    status: [{ required: true, message: "请选择职位状态", trigger: "blur" }],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询职位列表 */
function getList() {
  loading.value = true;
  listPost(queryParams.value).then((response) => {
    postList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}
/** 表单重置 */
function reset() {
  form.value = {
    postId: undefined,
    postCode: undefined,
    postName: undefined,
    postSort: 0,
    status: "1",
    remark: undefined,
  };
  proxy.resetForm("postRef");
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.postId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}
/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = $t("system.AddPost");
}
/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const postId = row.postId || ids.value;
  getPost(postId).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = $t("system.EditPost");
  });
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["postRef"].validate((valid) => {
    if (valid) {
      if (form.value.postId != undefined) {
        updatePost(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addPost(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

// -------------------------------------------------  删除事件  -----------------------------------------------------

const deleteId = ref();
const deleteName = ref();
const deleteVisible = ref(false);

const deletePrompt = (id, name) => {
  deleteVisible.value = true;
  deleteId.value = id;
  deleteName.value = name;
};

const closeDelete = () => {
  deleteVisible.value = false;
  deleteId.value = null;
  deleteName.value = null;
};

const deleteApply = async () => {
  const res = await delPost(deleteId.value);
  if (res.code === 200) ElMessage.success(res.msg);
  else ElMessage.error(res.msg);
  getList();
  deleteVisible.value = false;
  deleteId.value = null;
  deleteName.value = null;
};
/** 删除按钮操作 */
function handleDelete(row) {
  const postIds = row.postId || ids.value;
  proxy.$modal
    .confirm('是否确认删除职位编号为"' + postIds + '"的数据项？')
    .then(function () {
      return delPost(postIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}
/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "system/post/export",
    {
      ...queryParams.value,
    },
    `post_${new Date().getTime()}.xlsx`
  );
}

getList();
</script>

<style lang="scss" scoped>
.post {
  width: 100%;
  height: 100%;

  .container {
    position: relative;
    width: calc(100% - 40px);
    height: 92%;
    margin: 0 20px 20px;
    border-radius: 10px;
    background: white;

    .pagination {
      position: absolute;
      left: 50%;
      bottom: 30px;
      transform: translateX(-50%);
    }
  }

  .search-card {
    position: absolute;
    right: 0px;
    top: -45px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 40px;

    .el-button {
      width: 106px;
      height: 32px;
      opacity: 1;
      border-radius: 4px;
      background: rgba(43, 193, 123, 1);
      border: 1px solid rgba(43, 193, 123, 1);
      font-size: 14px;
      color: white;
    }
  }

  .tag {
    display: inline-block;
    width: 80px;
    height: 30px;
    line-height: 30px;
    text-align: center;
    border-radius: 15px;
    color: rgba(153, 153, 153, 1);
    border: 1px solid rgba(153, 153, 153, 1);
  }
  .tag.warning {
    color: rgba(255, 149, 18, 1);
    border: 1px solid rgba(255, 149, 18, 1);
  }
  .tag.success {
    color: rgba(43, 193, 123, 1);
    border: 1px solid rgba(43, 193, 123, 1);
  }
}
</style>
